const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


// app初始化
var app = getApp();  

const get = function (url, param) {

  console.log(app.globalData.host + url);
  return new Promise((resolve, reject) => {
    wx.request({
      url: app.globalData.host + url,
      data: param,
      method: 'GET',
      dataType: 'json',
      header: {
        "x-authtoken": wx.getStorageSync('token')
      },
      success: function (res) {

        if(res.data['code'] === 1){        

          wx.showModal({
            title: "错误提示",
            content: res.data['msg'],
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          });

          reject(res);
        }else{
          resolve(res);
         
        }
        
      },
      fail: function (err) {
        reject(err);
        wx.showModal({
          title: "错误提示",
          content: err.data['msg'],
          success: function (res) {
           
          }
        });
      }
    });
  });
};
const post = function (url, param) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: app.globalData.host + url,
      data: param,
      method: 'POST',
      dataType: 'json',
      header: {
        "x-authtoken": "L3k5RlRqUHI3RHJwMVUwamNYbVVDQT09OkxXSEdqbXJ4RVI3alZKZW1nQlU3TFE9PQ"
      },
      success: function (res) {
        if (res.data['code'] === 1) {

          wx.showModal({
            title: "错误提示",
            content: res.data['msg'],
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          });

          reject(res);
        } else {
          resolve(res);

        }
      },
      fail: function (err) {
        reject(err);
        wx.showModal({
          title: "错误提示",
          content: err.data['msg'],
          success: function (res) {
           
          }
        });
      }
    });
  });

}

module.exports = {
  formatTime: formatTime,
  get:get,
  post:post
}
